<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>后台登录</title>
    <link rel="stylesheet" type="text/css" href="/logView/layui/css/layui.css"/>
    <link rel="stylesheet" type="text/css" href="/logView/css/login.css"/>
</head>

<body>
<div class="m-login-bg">
    <div class="m-login">
        <h3>日志系统登录</h3>
        <div class="m-login-warp">
            <form class="layui-form">
                <div class="layui-form-item">
                    <input type="text" name="username" required lay-verify="username" placeholder="用户名" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <input type="password" name="password" required lay-verify="password" placeholder="密码" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <input type="text" name="verity" required lay-verify="required" placeholder="验证码" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-inline">
                        <img id="verifyImg" src="" alt="更换验证码" height="100%" width="100%" onclick="getVerify(this);">
                    </div>
                </div>
                <div class="layui-form-item m-login-btn">
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="login">登录</button>
                    </div>
                    <div class="layui-inline">
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
        <p class="copyright">Copyright logView</p>
    </div>
</div>
<script src="/logView/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form, layer = layui.layer;
        var $ = layui.$;

        if (window.localStorage) {
            //支持
        } else {
            layer.alert("浏览器不支持localStorage");
        }
        //验证码
        $('#verifyImg')[0].src = "/logView/createCode?key=" + guid();

        //自定义验证规则
        form.verify({
            username: function (value) {
                if (value.length < 5) {
                    return '用户名至少得5个字符';
                }
            },
            password: [/(.+){5,10}$/, '密码必须5到10位']

        });

        //监听提交
        form.on('submit(login)', function (data) {
            $.ajax({
                type: "post",    //请求方式
                async: true,    //是否异步
                url: "/logView/login",
                data: data.field,
                success: function (data) {
                    if (data.code == '200') {
                        layer.msg(data.msg);
                        window.location.href = "/logView/index";
                    } else {
                        layer.msg(data.msg);
                    }
                },
                error: function () {
                    layer.msg("登录失败");
                }
            });
            return false;
        });
    });

    function getVerify(obj) {
        obj.src = "/logView/createCode?key=" + guid();
    }

    //用于生成uuid
    function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }

    function guid() {
        return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
    }
</script>
</body>
</html>